﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;

using SignalRServerDemo.Hubs;

namespace SignalRServerDemo.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class CustomHubController : ControllerBase
    {
        private readonly IHubContext<CustomHub> _customHub;

        public CustomHubController(IHubContext<CustomHub> customHub)
        {
            _customHub = customHub;
        }

        /// <summary>
        /// 广播
        /// </summary>
        [HttpGet]
        public async Task<IActionResult> SendData()
        {
            await _customHub.Clients.All.SendAsync("client_recive", new TransData(Guid.NewGuid().ToString(), $"服务器主动发广播消息，当前时间：{DateTime.Now}"));
            return Ok();
        }

        /// <summary>
        /// 组播
        /// </summary>
        [HttpGet]
        public async Task<IActionResult> SendGroupData([FromQuery] string groupId)
        {
            await _customHub.Clients.Group(groupId).SendAsync("client_recive", new TransData(Guid.NewGuid().ToString(), $"服务器主动发组播消息，当前时间：{DateTime.Now}"));
            return Ok();
        }
    }
}
